﻿@using Telerik.Web.Mvc
@using Telerik.Web.Mvc.UI
@using Presentation.Web.Admin
@model  IEnumerable<Application.Admin.Dto.TourDetailDto>
@{
    ViewBag.subtitle = "线路景点管理";
    ViewBag.title = "线路景点管理";
    var targetId = 0;
    if (ViewBag.tour != null)
    {
        targetId = ViewBag.tour.Id;
        ViewBag.targetId = targetId;
        ViewBag.subtitle = "线路景点管理" + (string.IsNullOrEmpty(ViewBag.tour.Name) ? "" : "--" + ViewBag.tour.Name);
        ViewBag.title = "线路景点管理" + (string.IsNullOrEmpty(ViewBag.tour.Name) ? "" : "--" + ViewBag.tour.Name);
    }
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@{ Html.Telerik().Window()
        .Name("TourSightAddWindow")
        .Title("添加线路景点")
        .Content(@<text>
    @Html.Partial("_AddTourSight", new Application.Admin.Dto.TourDetailDto())
    </text>)
        .Width(700)
        .Draggable(true).Modal(true).Visible(false)
        .Render();
}
<input id = "targetId" name = "targetId" type = "hidden" value = "@(targetId)"/>
@{Html.Telerik().Grid(Model)
    .Name("TourSightGrid")
    .ToolBar(toolBar => toolBar.Template(
    @<text>
    @{
        if (Html.HasUiPermission(this.ViewContext, "addtourdetail"))
        { 
        <a href="javascript:;" id="addTourSight" class='t-button'>添加线路景点</a>
        }
        if (Html.HasUiPermission(this.ViewContext, "muldeletetourdetail"))
        { 
        <a href='javascript:;' class='t-button' onclick='mulDelete()'>删除选择项</a>
        }
    }
    <a href='/tour/' class='t-button' style='float: right;'>返回</a>
    </text>))
    .Columns(columns =>
    {
        columns.Template(@<text><input name="selectRow" type="checkbox" value="@item.Id " title="checkedRecords" /></text>)
        .HeaderTemplate(@<text><input type="checkbox" title="选择所有" id="checkAllRecords" /></text>)
        .Width(50).HeaderHtmlAttributes(new { style = "text-align:center" }).HtmlAttributes(new { style = "text-align:center" })
        .ClientTemplate("<input type=\"checkbox\" title=\"check all records\" name = 'selectRow' value = '<#= Id #>'/>");
        columns.Bound(o => o.Id).Width(200).Title("系统Id").Hidden();

        columns.Bound(o => o.SortNum).Width(30).Title("序号").ReadOnly();
        columns.Bound(o => o.SightName).Width(150).Title("景点名").ReadOnly();
        columns.Bound(o => o.SightId).Width(150).Title("景点Id").Hidden();
        columns.Bound(o => o.TourId).Width(150).Title("线路Id").Hidden();

        columns.Bound(o => o.Remark).Width(500).Title("描述").EditorTemplateName("TextArea");
        columns.Bound(o => o.NextTips).Width(200).Title("友情提示");
        columns.Bound(o => o.CustomDateTime).Width(100).Title("时间");
        columns.Bound(o => o.Day).Width(50).Title("第几天").EditorTemplateName("Int");
        columns.Bound(o => o.IsShowInMap).Width(100).Title("地图上显示");

        columns.Bound(o => o.IpAddress).Width(200).Title("添加IP").Hidden();
        columns.Bound(o => o.AddTime).Width(200).Title("添加时间").Hidden();
        columns.Bound(o => o.State).Width(200).Title("状态").Hidden();

        columns.Template(@<text><a  href = 'javascript:;'  class = 'tourSightUp t-button' rel = '@(item.Id)' >上移</a>
    <a  href = 'javascript:;'  class = 'tourSightDown t-button' rel = '@(item.Id)' >下移</a>
    </text>)
        .ClientTemplate("<a  href = 'javascript:;'  class = 'tourSightUp t-button' rel = '<#=Id #>' >上移</a> <a  href = 'javascript:;'  class = 'tourSightDown t-button' rel = '<#=Id #>' >下移</a> ")
        .Title("移动").Hidden(!Html.HasUiPermission(this.ViewContext, "toursightdown") || !Html.HasUiPermission(this.ViewContext, "toursightup"));

        columns.Command(comm =>
        {
            if (Html.HasUiPermission(this.ViewContext, "edittourdetail"))
            {
                comm.Edit().ButtonType(GridButtonType.Text);
            }
            if (Html.HasUiPermission(this.ViewContext, "deletetourdetail"))
            {
                comm.Delete().ButtonType(GridButtonType.Text);
            }
        })
        .Width(100)
        .Title("编辑/删除");
    })
    .ClientEvents(clientEvents => clientEvents.OnDataBinding("dataBinding")
        .OnDataBound("bindTourSightUpDown").OnComplete("bindComplete").OnRowDataBound("onRowBind"))
    .DataKeys(key => key.Add("Id"))//添加主键
    .DataBinding(dataBinding => dataBinding.Ajax().Select("ajaxtourdetailindex", "tour")
        .Update("edittourdetail", "tour", new { targetId = targetId })
        .Delete("deletetourdetail", "tour", new { targetId = targetId }))
    .Editable(editing => editing.Mode(GridEditMode.InLine))
    .Pageable(page => page.PageSize(15))
    .Groupable()
    .Render();
}
<script type="text/javascript">
    function dataBinding(args) {
        var targetId = $('#targetId').val();
        args.data = $.extend(args.data, { targetId: targetId });
    }

    function mulDelete() {
        var delStr = $(':checked , #targetId');
        if (delStr.length <= 1) {
            alert('亲，您没有选择任何记录.');
            return;
        }
        jQuery.post('/tour/muldeletetourdetail', delStr, function (res) {
            if (res.success) {
                setTimeout(function () {
                    $("#TourSightGrid").data("tGrid").rebind();
                    $("#checkAllRecords").attr("checked", false);
                }, 1000);
            } else {
                alert(res.message);
            }
        });
    }

    function bindTourSightUpDown(d) {
        $('.tourSightUp').bind('click', function (e) {
            var id = $(this).attr('rel');
            var targetId = $('#targetId').val();
            var data = { id: id, targetId: targetId };
            $.post('/tour/toursightup', data, function (res) {
                if (res.success) {
                    $("#TourSightGrid").data("tGrid").rebind();
                }
                else {
                    alert(res.message);
                }
            });
        });

        $('.tourSightDown').bind('click', function (e) {
            var id = $(this).attr('rel');
            var targetId = $('#targetId').val();
            var data = { id: id, targetId: targetId };
            $.post('/tour/toursightdown', data, function (res) {
                if (res.success) {
                    $("#TourSightGrid").data("tGrid").rebind();
                }
                else {
                    alert(res.message);
                }
            });
        });
    }

    function onRowBind(e) {
        var columns = $(e.row.outerHTML).children();
        var colStr = "";
        var count = 0;
        $.map(columns, function (item) {
            if (count == 10) {
                if (e.dataItem.IsShowInMap) {
                    $(item).html('<input type="checkbox" disabled="disabled" class="check-box" checked="checked">');
                }
                else {
                    $(item).html('<input type="checkbox" disabled="disabled" class="check-box">');
                }
            }
            colStr += item.outerHTML;
            count++;
        })
        e.row.innerHTML = colStr;
    }

    function bindComplete(e) {
        $("input:checkbox[name='selectRow']").attr("checked", false);
        var success = e.response.success;
        if (success != null && success != undefined) {
            alert(e.response.message);
            window.location.reload();
        }
    }

    $(function () {
        bindTourSightUpDown();
        $("input:checkbox[name='selectRow']").attr("checked", false);
        $('#addTourSight').bind('click', function (e) {
            e.preventDefault();
            $('#TourSightAddWindow').data('tWindow').center().open();
        });

        $('#checkAllRecords').click(function checkAll() { $("#TourSightGrid tbody input:checkbox[name='selectRow']").attr("checked", this.checked); });
        $(":input[data-autocomplete]").each(function () {
            $(this).autocomplete({
                source: $(this).attr("data-autocomplete")
            });
        });

        $("#searchTour").focus(function () {
            if (this.value == "线路名") {
                $("#searchTour").val("");
            }
        }).blur(function () {
            if (this.value == "") {
                $("#searchTour").val("线路名");
            }
        });
    });
</script>
